package com.cloudloan.controller;

import com.cloudloan.common.R;
import com.cloudloan.entity.User;
import com.cloudloan.service.LoginServcie;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value = "用户服务",tags = "用户服务")
public class LoginController {
    @Autowired
    private LoginServcie loginServcie;
    @PostMapping("/login")
    @ApiOperation("web端登录接口")
    public R login(@RequestBody User user){
        return loginServcie.login(user);
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/loginOut")
    public R logout(){
        return loginServcie.logout();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/id")
    public R getUserId(@RequestParam Long id){
        return loginServcie.getUserId(id);
    }

    /**
     * 保存用户
     * @param user
     * @return
     */
    @PostMapping("/sava")
    public R savaUser(@RequestBody User user){
        if (user.getPassword()==null||user.getPassword().isBlank()) {
            return R.error("密码设置不正确");
        }
        return loginServcie.saveUser(user);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("/update")
    public R updateUser(@RequestBody User user){
        if (user.getPassword()!=null&&user.getPassword().isBlank()){
            return R.error("密码设置不正确");
        } else if (user.getId()==null){
            return R.error("未传递用户id");
        }else{
            user.setPassword(user.getPassword().trim());
        }
        return loginServcie.updateUser(user);
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id){
        return loginServcie.deleteById(id);
    }

    /**
     * 分页查询
     * @param page0
     * @param limit
     * @param map
     * @return
     */
    @GetMapping("/page")
    public R selectPage(@RequestParam(defaultValue = "1") Integer page0
            , @RequestParam(defaultValue = "10") Integer limit
    , @RequestParam Map<String,Object> map){
        return loginServcie.selectPage(page0,limit,map);
    }


}
